﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace code
{
    public partial class Form1 : Form
    {
        private string Path1,Path2,Path3 = ".\\Data";
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileName = new OpenFileDialog();//定义一个“文件打开”控件
            fileName.InitialDirectory = Application.StartupPath;//设置打开后的初始目录为当前exe所在目录
            fileName.Filter = "txt files (*.txt)|*.txt";//只可选择txt文件，避免后续文件读写引发异常
            if(fileName.ShowDialog() == DialogResult.OK)
            {
                Path1 = fileName.FileName;
                textBox1.Text = Path1;//在空间中展示文件目录，方便用户使用
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileName = new OpenFileDialog();
            fileName.InitialDirectory = Application.StartupPath;
            fileName.Filter = "txt files (*.txt)|*.txt";
            if (fileName.ShowDialog() == DialogResult.OK)
            {
                Path2 = fileName.FileName;
                textBox2.Text = Path2;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.InitialDirectory = Application.StartupPath + "\\Data";
            saveFile.Filter = "txt files |*.txt";
            saveFile.FileName = "C";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                Path3 = saveFile.FileName;
                textBox3.Text = Path3;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(".\\Data"))//保证当前可执行文件所在目录下，有子目录Data
            {
                Directory.CreateDirectory(".\\Data");
            }
            saveFile(Path3+"\\C.txt");
        }
        private void saveFile(string savePath)
        {
            try
            {
                FileStream fin1 = new FileStream(Path1, FileMode.Open, FileAccess.Read);
                FileStream fin2 = new FileStream(Path2, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(savePath, FileMode.Create, FileAccess.Write);
                StreamReader brin1= new StreamReader(fin1, System.Text.Encoding.UTF8);
                StreamReader brin2 = new StreamReader(fin2, System.Text.Encoding.UTF8);
                StreamWriter brout = new StreamWriter(fout, System.Text.Encoding.UTF8);

                int cnt = 0;//标记行号
                string str1 = brin1.ReadLine();
                string str2 = brin2.ReadLine();

                while (str1!=null)//先后读入fin1和fin2
                {
                    cnt++;
                    brout.WriteLine("文件1第"+cnt+"行：\t"+str1);
                    str1 = brin1.ReadLine();
                }
                brin1.Close();//关闭缓冲读入流和文件读入流的连接
                cnt = 0;//重置计数
                while (str2 != null)//先后读入fin1和fin2
                {
                    cnt++;
                    brout.WriteLine("文件2第" + cnt + "行：\t" + str2);
                    str2 = brin2.ReadLine();
                }
                MessageBox.Show("完成！！！");
                brin2.Close();
                brout.Close();
            }
            catch (ArgumentNullException e1)
            {
                MessageBox.Show("文件未找到！！！");
            }
            catch (IOException e2)
            {
                MessageBox.Show(e2.ToString());
            }
        }
    }
}
